import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_api_path.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/manager/network/xindong_networking.dart';
import 'package:xindong/models/xindong_file_select_class.dart';
import 'package:xindong/utils/xindong_text_util.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/common/xindong_button.dart';
import 'package:xindong/widgets/image/xindong_media_add_container.dart';

class XinDongFeedbackYeMian extends StatefulWidget {
  const XinDongFeedbackYeMian({super.key});

  @override
  State<XinDongFeedbackYeMian> createState() => _XinDongFeedbackYeMianState();
}

class _XinDongFeedbackYeMianState extends State<XinDongFeedbackYeMian> {
  List<String> tagList = ["功能建议", "Bug反馈", "内容建议", "体验建议", "其他建议"];
  int currentIndex = 0;
  final textEditingController = TextEditingController();

  // 图片数组
  List<XinDongSelectFileClass>? imageFileClassList;

  // 发布
  void onSubmit() async {
    final content = textEditingController.text.trim();
    if (TextUtil.isEmpty(content)) {
      showToast("你还没有填写意见哦");
      return;
    }
    showLoading();

    var images = '';
    if (imageFileClassList != null && imageFileClassList!.isNotEmpty) {
      List<String> filePaths = imageFileClassList!.map((e) => e.file.path).toList();
      String? result = await XinDongNetworking.instance.uploadFiles(filePaths: filePaths);
      if (result == null) {
        hideLoading();
        return;
      }
      images = result;
    }

    var parameters = {'type': tagList[currentIndex], "content": content, "img": images};
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userFeedback,
      method: XinDongHttpMethod.post,
      parameters: parameters,
    );
    hideLoading();
    if (object.code == XinDongServerStatus.success) {
      Get.back();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: XinDongAppBar("投诉与建议"),
      resizeToAvoidBottomInset: false,
      body: GestureDetector(
        onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
        child: Stack(
          children: [
            ListView(
              children: [
                Container(
                  height: 40,
                  alignment: Alignment.centerLeft,
                  padding: const EdgeInsets.only(left: kPaddingValue, right: kPaddingValue),
                  child: const Text(
                    "请选择您想反馈的功能点",
                    style: TextStyle(fontSize: 14, color: DZColors.titleColor),
                  ),
                ),
                Container(
                  color: DZColors.cellColor,
                  padding: const EdgeInsets.all(kPaddingValue),
                  child: Wrap(
                    spacing: 10,
                    runSpacing: 10,
                    children: tagList.map((e) => _buildTagItem(e)).toList(),
                  ),
                ),
                const SizedBox(height: 10),
                Container(
                  height: 138,
                  color: DZColors.cellColor,
                  child: TextField(
                    keyboardType: TextInputType.multiline,
                    maxLines: null,
                    // 不限制行数
                    style: const TextStyle(color: DZColors.titleColor, fontSize: 15.0, fontWeight: FontWeight.normal),
                    controller: textEditingController,
                    decoration: const InputDecoration(
                      hintText: "请填写你要反馈的问题",
                      hintMaxLines: 2,
                      hintStyle: TextStyle(fontSize: 15, color: DZColors.placeholderColor),
                      contentPadding: EdgeInsets.symmetric(vertical: kPaddingValue, horizontal: kPaddingValue),
                      // 内容偏移上下15，左右16
                      border: OutlineInputBorder(borderSide: BorderSide.none),
                    ),
                  ),
                ),
                Container(
                  height: 40,
                  alignment: Alignment.centerLeft,
                  padding: const EdgeInsets.only(left: kPaddingValue, right: kPaddingValue),
                  child: const Text(
                    "相关问题的截图和照片(选填)",
                    style: TextStyle(fontSize: 14, color: DZColors.titleColor),
                  ),
                ),
                Container(
                  color: DZColors.cellColor,
                  padding: const EdgeInsets.all(kPaddingValue),
                  child: XinDongMediaAddContainer(
                    maxCount: 3,
                    columns: 4,
                    supportType: MediaSupportType.onlyImage,
                    onSelect: (images, video) {
                      imageFileClassList = images;
                    },
                  ),
                ),
                const SizedBox(height: 120),
              ],
            ),
            Positioned(
              bottom: 48,
              left: 16,
              right: 16,
              child: SizedBox(
                height: 48,
                width: screenWidth,
                child: XinDongCommonButton("提交", onTap: onSubmit),
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget _buildTagItem(String text) {
    final isSelect = text == tagList[currentIndex];
    return GestureDetector(
      onTap: isSelect
          ? null
          : () {
              setState(() {
                currentIndex = tagList.indexOf(text);
              });
            },
      behavior: HitTestBehavior.opaque,
      child: Container(
        height: 30,
        padding: const EdgeInsets.only(left: 20, right: 20),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(30),
          // 渐变色
          gradient: LinearGradient(
            begin: Alignment.centerLeft,
            end: Alignment.centerRight,
            colors: isSelect
                ? [const Color.fromARGB(255, 121, 90, 205), const Color.fromARGB(255, 172, 90, 205)]
                : [const Color.fromARGB(255, 233, 233, 233), const Color.fromARGB(255, 233, 233, 233)],
          ),
        ),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              text,
              style: TextStyle(fontSize: 13, color: isSelect ? Colors.white : DZColors.titleColor, height: 1.2),
            )
          ],
        ),
      ),
    );
  }
}
